layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--角色管理
     */
    var SetAdvert = {
        tableId: "setAdvertTable",    //表格id
        condition: {
        	nickname: ""
        }
    };

    /**
     * 初始化表格的列
     */
    SetAdvert.initColumn = function () {
        return [[
        	{type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'url',width: 200,align:'center', templet:'#imgTpl', title: '图片'},
        	{field: 'fileName', sort: true, title: '文件名'},
        	{field: 'fileSize', sort: true, title: '文件大小'},
        	{field: 'describeStr', sort: true, title: '文件描述'},
            {field: 'fileType', sort: true, title: '文件类型'},
            {field: 'createUser', sort: true, title: '创建人'},
            {field: 'createTime', sort: true, title: '创建时间'}
        ]];
    };

    

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SetAdvert.tableId,
        url: Feng.ctxPath + '/advert/list',
        page: true,
        height: "full-158",
        limit:100,
        limits: [100,300,500],
        cellMinWidth: 100,
        cols: SetAdvert.initColumn()
    });
    
    SetAdvert.openAddSetAdvert = function () {
        var checkRows = table.checkStatus(SetAdvert.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择您要展示的广告!");
        } else {
        	var data = checkRows.data;
        	var advertIds = "";
        	var cabinetName = Feng.getUrlParam("cabinetName");
        	for (i = 0; i < data.length; i++) {
        		advertIds += data[i].id + ",";
        	}
        	advertIds = advertIds.substring(0, advertIds.lastIndexOf(','));
        	$.ajax({
    			url : Feng.ctxPath + "/advert/setAdvertAdd",
    			data : {
    				"advertIds" : advertIds,
    				"cabinetName" : cabinetName
    			},
    			type : 'post',
    			dataType : 'json',
    			beforeSend: function (request) {
    				layerIndex = layer.load(2, 
    					{
    						time: 3*1000
    						,content: '正在设置...'
    						, success: function (layero) {
    					        layero.find('.layui-layer-content').css({
    					            'padding-left': '40px',
    					            'width': '100px'
    					        });
    				    	}
    					}
    				);
    			},
    			success : function(data) {
    				layer.close(layerIndex);
    				Feng.success("设置成功!");
                    parent.layer.close(ZTreeDlg.index);
    			},
    			error : function(data) {
    				 Feng.error("设置失败!" + data.responseJSON.message + "!");
    			}
            });
        }
    };

    // 添加按钮点击事件
    $('#setAdvertId').click(function () {
    	SetAdvert.openAddSetAdvert();
    });
    // 工具条点击事件
    table.on('tool(' + SetAdvert.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'del') {
        	SetAdvert.openDelAdvert(data);
        }else if(layEvent === 'subSetAdvertList'){
        	SetAdvert.openSubAdvertList(data);
        }
    });
});
